<?php
App::import('Controller','Config.BaseConfig');
class SiteConfigController extends BaseConfigController
{
	var $name = 'SiteConfig';
	var $uses = array('ListingConfig','ReviewConfig','GeneralConfig');
	
	function admin_setting($params = null)
	{		
		$this->pageTitle = sprintf(__('%s Settings'),__($params));
		$modelName = $params.'Config';
		if(!in_array($modelName,$this->_getConfigurableModels()))
		{
			$this->errorMsg(__('Invalid URL!'));
			$this->redirect(Configure::read('Backend.adminLoginRedirect'));
		}
		$this->_processLogo();
		$this->_processFavIcon();
		
		$this->configModelAlias = $modelName;
		parent::admin_setting($params);
		
	}
	
	function _getConfigurableModels()
	{
		$configurableModels = array('ListingConfig','ReviewConfig','GeneralConfig');
		return $configurableModels;
	}
	
	function _processLogo()
	{
		if(!empty($this->data) && isset($this->data['GeneralConfig']))
		{	
			if(isset($this->data['GeneralConfig']['logo']) && $this->data['GeneralConfig']['logo']['name'] == '')
			{
				unset($this->data['GeneralConfig']['logo']);
			}elseif(isset($this->data['GeneralConfig']['logo']))
			{		
				$this->GeneralConfig->set($this->data);
				if($this->GeneralConfig->validates())
				{
					$source = $this->data['GeneralConfig']['logo']['tmp_name'];
					$destination = WWW_ROOT.DS.'uploads'.DS.'logo'.DS.$this->data['GeneralConfig']['logo']['name'];
					if(move_uploaded_file($source, $destination))
					{
						$logoImage = $this->data['GeneralConfig']['logo']['name'];
						unset($this->data['GeneralConfig']['logo']);
						$this->data['GeneralConfig']['logo'] = $logoImage;
					}
				}		
			}					
		}
	}
	
	function _processFavIcon()
	{
		if(!empty($this->data) && isset($this->data['GeneralConfig']))
		{	
			if(isset($this->data['GeneralConfig']['fav_icon']) && $this->data['GeneralConfig']['fav_icon']['name'] == '')
			{
				unset($this->data['GeneralConfig']['fav_icon']);
			}elseif(isset($this->data['GeneralConfig']['fav_icon']))
			{		
				$this->GeneralConfig->set($this->data);
				if($this->GeneralConfig->validates())
				{
					$source = $this->data['GeneralConfig']['fav_icon']['tmp_name'];
					$destination = WWW_ROOT.'favicon.ico';
					if(move_uploaded_file($source, $destination))
					{
						$favIconImage = $this->data['GeneralConfig']['fav_icon']['name'];
						unset($this->data['GeneralConfig']['fav_icon']);
						$this->data['GeneralConfig']['fav_icon'] = $favIconImage;
					}
				}		
			}					
		}
	}
}
?>